package com.anolesoft.yr.mbus.cont.service.impl;

import com.anolesoft.yr.mbus.cont.dao.ContContentDao;
import com.anolesoft.yr.mbus.cont.dao.ContMainDao;
import com.anolesoft.yr.mbus.cont.dao.ContPaymentDao;
import com.anolesoft.yr.mbus.cont.dao.ContVariableDao;
import com.anolesoft.yr.mbus.cont.dto.ContMainDTO;
import com.anolesoft.yr.mbus.cont.entity.*;
import com.anolesoft.yr.mbus.cont.service.ContMainAddService;
import com.anolesoft.yr.mbus.cont.service.ContWordCreatService;
import com.anolesoft.yr.mbus.contm.dao.ContmContentDao;
import com.anolesoft.yr.mbus.contm.entity.ContmContent;
import com.anolesoft.yr.mbus.contm.entity.ContmContentExample;
import com.anolesoft.yr.mbus.contm.entity.ContmMain;
import com.anolesoft.yr.mbus.contm.dao.ContmMainDao;
import com.anolesoft.yr.mbus.contm.util.StringUtil;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@AllArgsConstructor
@Service
public class ContMainAddServiceImpl implements ContMainAddService {

    private ContmMainDao contmMainDao;
    private ContContentDao contContentDao;
    private ContMainDao contMainDao;
    private ContmContentDao contmContentDao;
    private ContPaymentDao contPaymentDao;
    private ContVariableDao contVariableDao;
    private ContWordCreatService contWordCreatService;

    /**
     *
     * @param preOrderObjuid
     * @param contmMainObjuid
     * @param contMainDto
     */
    public void contMainAdd(String preOrderObjuid, String contmMainObjuid, ContMainDTO contMainDto){
        ContmMain contmMain = contmMainDao.selectById(contmMainObjuid);
        String contMainObjuid = StringUtil.generateRundom32String();
        ContMain contMain = new ContMain();
        contMain.setObjuid(contMainObjuid);
        List<ContmContent> contmContents = contmMain.getContmContents();
        List<ContContent> contContents = new ArrayList<>();
        contmContents.forEach(a->{
            contContents.add(new ContContent(a, contmMain, new ContContent()));
        });
        contMain.setContContents(contContents);
        ContMainInsert(contMain);
    }

    public void ContMainInsert(ContMain contMain){
        contMainDao.insert(contMain);
        List<ContContent> contContents = contMain.getContContents();
        contContents.forEach(a->{
            contContentInsert(a);
        });
    }

    public void contContentInsert(ContContent content){
        contContentDao.insert(content);
        List<ContContent> contContents = content.getContContents();
        contContents.forEach(a->{
            contContentInsert(a);
        });
    }



    @Override
    public void add(ContMain contMain) {
        contMainDao.insert(contMain);
        contMain.getContPayments().forEach(contPayment -> {
            contPayment.setObjuid(StringUtil.generateRundom32String());
            contPaymentDao.insert(contPayment);
        });

        String contMUid = contMain.getContTemplateUid();
        ContmContentExample contmContentExample = new ContmContentExample();
        contmContentExample.createCriteria().andContmMainUidEqualTo(contMUid);
        List<ContmContent> contmContents = contmContentDao.selectByExample(contmContentExample);
        Map<String,String> keyMap = new HashMap<>();
        for (int i = 0; i < contmContents.size(); i++) {
             String objuidTemp = contmContents.get(i).getObjuid();
             String objuid = StringUtil.generateRundom32String();
             keyMap.put(objuidTemp,objuid);
             contmContents.get(i).setObjuid(objuid);
        }

        for (int i = 0; i < contmContents.size(); i++) {
            if (contmContents.get(i).getParentUid()!=null&&!"".equals(contmContents.get(i).getParentUid())){
                contmContents.get(i).setParentUid(keyMap.get(contmContents.get(i).getParentUid()));
            }
        }
        List<ContContent> contContents = Lists.newArrayList();

        for (int i = 0; i < contmContents.size(); i++) {
            contContents.add(contmContents.get(i).toContContent(contMain.getObjuid()));
        }

        contContentDao.batchInsert(contContents);




        /**
         * 等着曹哥的报价信息和产品信息才可以继续写了
         */
    }

    private void contContmentLoop(List<ContmContent> contmContents,String parentUid,String contMainUid){
        for (int i = 0 ; i<contmContents.size();i++){
            ContmContent contmContent = contmContents.get(i);
            String objuid = StringUtil.generateRundom32String();
            ContContent contContent = ContContent.builder().content(contmContent.getContent()).contmStyleUid(contmContent.getStyleItemUid()).parentUid(parentUid).objuid(objuid).contMainUid(contMainUid).orderCode(Byte.parseByte(Integer.toString(i))).pageType(contmContent.getPageType()).itemType(contmContent.getItemType()).build();
            contContentDao.insert(contContent);
          //  List<ContmContent> contmContentsTypeOfTitle = contmContent.getContmContentsTypeOfTitle();
          //  List<ContmContent> contmContentsTypeOfContent = contmContent.getContmContentsTypeOfContent();
          //  contContmentLoop(contmContentsTypeOfTitle,objuid,contMainUid);
          //  contContmentLoop(contmContentsTypeOfContent,objuid,contMainUid);
        }
    }

    /**
     * 这个里边不能考虑到表的变量
     * @param contMain
     * @return
     */
    public Map<String, String> getContValue(ContMain contMain) {
        //获取所有变量
        List<ContVariable> contVariables = contVariableDao.selectByExample(new ContVariableExample());
        //将合同表的字段名与字段值形成一个map
        Map<String, String> map = contMain.toMap();
        //过滤出来都有哪些值是从m合同表中获取的
        List<ContVariable> contList = contVariables.stream().filter(a -> a.getVarType().equals("java")).collect(Collectors.toList());
        //建立结果map
        Map<String, String> resultMap = new HashMap<>();
        //确定map中都有哪些key 省的再取值的时候报错
        List<String> collect = map.keySet().stream().collect(Collectors.toList());
        //首先将合同表里的数据存进来
        contList.forEach(a->{
            if (collect.contains(a.getVarVal())){
                resultMap.put(a.getVarName(),map.get(a.getVarVal()));
            }
        });


        return resultMap;
    }


    @Override
    public void update(ContMain contMain){
        contMainDao.updateById(contMain);
        ContPaymentExample contPaymentExample = new ContPaymentExample();
        contPaymentExample.createCriteria().andContMainUidEqualTo(contMain.getObjuid());
        contPaymentDao.deleteByExample(contPaymentExample);
        contMain.getContPayments().forEach(a->{
            contPaymentDao.insert(a);
        });
    }


}
